<template>
  <j-modal
    :title="title"
    :width="1300"
    :visible="visible"
    :maskClosable="false"
    :ok-button-props="{ props: { disabled: disableSubmit } }"
    @close="close"
    cancelText="关闭"
  >
    <div style="margin: 0 auto" :class="{ isCheck: isCheck }">
      <a-form-model ref="form" :model="model" :rules="rules" :label-col="labelCol" :wrapper-col="wrapperCol">
        <a-row>
          <a-col :span="12">
            <a-form-model-item label="问题分类" prop="quatypes">
              <a-tree-select
                :replaceFields="{children:'children', title:'name', key:'id', value: 'id' }"
                v-model="model.quatypes"
                style="width: 100%"
                :tree-data="treeData"
                tree-checkable
                :disabled="disableSubmit"
              />
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="取证编码">
              {{ model.number? model.number:'系统生成' }}
            </a-form-model-item>
          </a-col>
        </a-row>
        <a-row>
          <a-col :span="12">
            <a-form-model-item label="审计人员">
              <a-input v-model='model.userName' :disabled='true'/>
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="取证时间" prop="forensicsDate">
              <j-date style='width:100%' v-model='model.forensicsDate' :disabled='disableSubmit'></j-date>
            </a-form-model-item>
          </a-col>
        </a-row>
        <a-row>
          <a-col :span="12">
            <a-form-model-item label="证据提供单位" prop="unitName">
              <a-input v-model='model.unitName' :disabled='disableSubmit'/>
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="证据提供日期" prop="provideDate">
              <j-date style='width: 100%' v-model='model.provideDate' :disabled='disableSubmit'></j-date>
            </a-form-model-item>
          </a-col>
        </a-row>
        <a-row>
          <a-form-model-item label="取证事项" :label-col="{span:2}" :wrapper-col="{span:20}">
            <a-textarea allow-clear v-model="model.matter" :disabled='disableSubmit' />
          </a-form-model-item>
        </a-row>
        <a-row>
          <a-form-model-item label="定性依据" :label-col="{span:2}" :wrapper-col="{span:20}" prop="accord">
            <a-select
              v-model="model.accord"
              mode="tags"
              style="width: 100%"
              option-label-prop="label"
              :disabled='disableSubmit'
            >
              <template v-for="(item,index) in regulation">
                <a-select-option :value="item.title||''" :label="item.title" :key="id">
                  {{item.title}}
                </a-select-option>
              </template>

            </a-select>
          </a-form-model-item>
        </a-row>
        <a-row>
          <a-form-model-item label="法规制度" :label-col="{span:2}" :wrapper-col="{span:20}">
            <RuleUpload :isShow="disableSubmit" ref='fileUpload' :fileid='model.fileId'
                        @uploadFile="uploadFile"></RuleUpload>
          </a-form-model-item>
        </a-row>
      </a-form-model>
    </div>
    <!-- table区域-end -->
  </j-modal>
</template>
<script>
  import {postAction, getAction} from '@/api/manage'
  import RuleUpload from '../../../components/RuleUpload'
  import store from '@/store/'

  export default {
    name: 'SysPositionModal',
    props: ['id', 'bsjdwName', 'bsjdw', 'protype', 'type'],
    components: {
      RuleUpload
    },
    data() {
      return {
        labelCol: {span: 4},
        wrapperCol: {span: 18},
        disableSubmit: false,
        title: '',
        visible: false,
        isCheck: false,
        model: {},
        queryParam: {},
        regulation: [],
        treeData: [],
        userInfo: store.getters.userInfo,
        rules: {
          // quatypes: [{required: true, message: '不能为空!'}],
          forensicsDate: [{required: true, message: '不能为空!'}],
          unitName: [{required: true, message: '不能为空!'}],
          provideDate: [{required: true, message: '不能为空!'}],
          accord: [{required: true, message: '不能为空!'}],
        },
        url: {
          regulation: '/sjzszhly/knowledgeRegulation/queryKnowledgeRegulation',
          save: '/sjxmzygl/sjWorkZtfaMatter/save',
          tree: '/sys/category/problemTreeBytype',
          get: '/sjxmzygl/sjWorkZtfaMatter/queryProjectMatterById'
        },
      }
    },
    created() {
      this.getRegulation()
      this.loadTree()
    },
    watch: {},
    methods: {
      check(record) {
        this.queryParam = record
        this.getQzData()
        this.visible = true
      },
      edit(record) {
        this.queryParam = record
        this.visible = true
        this.getQzData()
      },
      getRegulation() {
        getAction(this.url.regulation).then((res) => {
          if (res.success && res.result.length > 0) {
            this.regulation = res.result
          } else {
            console.log(res)
          }
        })
      },
      close(isSubmit) {
        if (isSubmit) {
          this.saveData()
        } else {
          this.visible = false
        }
      },
      saveData() {
        this.$refs.form.validate((valid) => {
          if (valid) {
            this.$refs.fileUpload.saveFile('sjqz')
          } else {
            return false
          }
        })
      },
      loadTree() {
        this.treeData = []
        getAction(this.url.tree, {protype: this.protype}).then((res) => {
          if (res.success) {
            this.treeData = res.result
          }
        })
      },
      uploadFile(id) {
        this.model.fileId = id
        this.$refs.form.validate((valid) => {
          if (valid) {
            this.model.accord = this.model.accord.toString()
            this.model.quatypes = this.model.quatypes.toString()
            postAction(this.url.save, {
              ...this.model,
              proId: this.queryParam.proId,
              problemId: this.queryParam.digaoId
            }).then((res) => {
              if (res.success) {
                this.$message.success(res.message)
                this.$emit('ok')
                this.visible = false
              } else {
                this.$message.error(res.message)
              }
            })
          } else {
            return false
          }
        })
      },
      getQzData() {
        if (this.queryParam.id) {
          getAction(this.url.get, {id: this.queryParam.id}).then((res) => {
            if (res.success && res.result[0]) {
              this.model = res.result[0]
              delete this.model.problemId
              if (this.model.accord) {
                this.model.accord = this.model.accord.split(',');
              } else {
                this.model.accord = []
              }
              if (this.model.quatypes) {
                this.model.quatypes = this.model.quatypes.split(',');
              } else {
                this.model.quatypes = []
              }
            } else {
              this.model = {}
              this.model.userName = this.userInfo.realname
              console.log(res)
            }
          })
        } else {
          this.model = {}
        }
      }
    },
  }
</script>

<style lang="less" scoped>

  .name-type {
    width: 110px;
    margin-left: 5px;
    border: 1px solid #e0e0e0;

    /deep/ .ant-select-selection {
      box-sizing: border-box;
      min-height: 27px !important;

      .ant-select-selection__rendered {
        line-height: 27px !important;
      }
    }
  }
</style>
